<?php

namespace App\Http\Controllers;

use App\OrderGoods;
use Illuminate\Http\Request;

class OrderGoodsController extends Controller
{
    public function getAll($company) {
        $goods=OrderGoods::where('company',$company)->get();
        return response()->json($goods);
    }
    public function getCurrent($page,$company) {
        $pages=$page*10-10;
        $goods=OrderGoods::where('company',$company)->skip($pages)->take(10)->get();
        return response()->json($goods);
    }
    public function one($id) {
        $goods=OrderGoods::find($id);
        return response()->json($goods);
    }

    public function getCount($company) {
        $goods=OrderGoods::where('company',$company)->count();
        return response()->json($goods);
    }
    public function add( Request $request) {

        $add=$request->except('_token');
        $add['code']='code'.time();
        $goods=OrderGoods::create($add);
        if($goods){
            return response()->json('添加成功');
        }else{
            return response()->json('添加失败');
        }

    }
    public function del ( Request $request) {
        $date=$request->input('date');

        foreach ($date as $d){
            OrderGoods::destroy($d['id']);
        }
        return response()->json('删除成功');

    }
    public function update($id, Request $request) {
        $edit=$request->except('_token');
        OrderGoods::where('id',$id)->update($edit);
        return response()->json('修改成功');
    }
    public function search($word) {
        $goods=OrderGoods::where('name','like','%'.$word.'%')->OrWhere('email','like','%'.$word.'%')->get();
        return response()->json($goods);
    }
    public function page($page,$pagesize) {
        $skip=$page*$pagesize;
        $take=$pagesize;
        $goods=OrderGoods::orderBy('id','desc')->skip($skip)->take($take)->get();
        return response()->json($goods);
    }
    public function dateOne($date) {

        $goods=OrderGoods::where('created_at',$date)->get();
        return response()->json($goods);
    }
    public function datemore($date) {

        $goods=OrderGoods::where('created_at',$date)->get();
        return response()->json($goods);
    }
}
